package net.suncaper.demo.service;

import net.suncaper.demo.domain.User;
import net.suncaper.demo.domain.UserExample;
import net.suncaper.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;


    @Override
    public List<User> findUser() {
        return userMapper.selectByExample(new UserExample());
    }

    @Override
    public void saveUser(User user) {
        userMapper.insert(user);
    }

    @Override
    public  int checkUser(User user)
    {
        List<User>users=userMapper.selectByTele("%" + user.getTelephone() + "%");
        if(users.isEmpty()){
            return 1; //1为用户不存在
        }

        else{
            User result=users.get(0);

            if(result.getPassword().equals(user.getPassword()))
            {
                return 2;//2为登录成功
            }
            else
            {
                return 3;//3为用户存在，密码错误
            }
        }
    }

    @Override
    public List<User> selectByTele(User user){
        return userMapper.selectByTele("%" + user.getTelephone() + "%");
    }

    @Override
    public User selectByPrimaryKey(Long userid){
        return userMapper.selectByPrimaryKey(userid);
    }

    @Override
    public void updateByID(Long userID, String name, String sex, String birthday) {
        userMapper.updateByID(userID,name,sex,birthday);
    }
}
